package jmu.rzk.shippingmanagementsystem.controller;

import jakarta.validation.Valid;
import jmu.rzk.shippingmanagementsystem.entity.User;
import jmu.rzk.shippingmanagementsystem.service.UserService;
import jmu.rzk.shippingmanagementsystem.vo.request.LoginRequest;
import jmu.rzk.shippingmanagementsystem.vo.request.RegisterRequest;
import jmu.rzk.shippingmanagementsystem.vo.response.UserResponse;
import jmu.rzk.shippingmanagementsystem.vo.response.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 用户注册
     * @param registerRequest 注册请求
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result<UserResponse> register(@Valid @RequestBody RegisterRequest registerRequest) {
        UserResponse userResponse = userService.register(registerRequest);
        return Result.success("注册成功", userResponse);
    }
    
    /**
     * 用户登录
     * @param loginRequest 登录请求
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result<UserResponse> login(@Valid @RequestBody LoginRequest loginRequest) {
        UserResponse userResponse = userService.login(loginRequest);
        return Result.success("登录成功", userResponse);
    }
    
    /**
     * 获取用户信息
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public Result<UserResponse> getUserById(@PathVariable Long id) {
        UserResponse userResponse = userService.getUserById(id);
        return Result.success(userResponse);
    }
    
    /**
     * 更新用户信息
     * @param id 用户ID
     * @param user 用户信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public Result<UserResponse> updateUser(@PathVariable Long id, @RequestBody User user) {
        UserResponse userResponse = userService.updateUser(id, user);
        return Result.success("更新成功", userResponse);
    }
    
    /**
     * 删除用户
     * @param id 用户ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success("删除成功", null);
    }
    
    /**
     * 获取所有用户
     * @return 用户列表
     */
    @GetMapping("/list")
    public Result<List<UserResponse>> getAllUsers() {
        List<UserResponse> userResponses = userService.getAllUsers();
        return Result.success(userResponses);
    }
} 